﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;

namespace Xenta.Web.Controllers
{
    public sealed class ArticleController : ExtController
    {
        [HttpGet]
        public ActionResult Index()
        {
            var m = Svc.ProcessOperation("SearchPublications", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                PublishedTo = DateTime.UtcNow,
                FlagMask = "Active,Article",
                SortBy = new[] 
                {
                    "PublishedOn"
                },
                SortDir = "Descending"
            }, new
            {
                DisclosureLevel = "Base",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            return View(m);
        }

        [HttpGet, RestoreState]
        public ActionResult Show(int id)
        {
            var m = Svc.ProcessOperation("GetPublication", new
            {
                EntityID = id
            }, new
            {
                DisclosureLevel = "Entire",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            if(m == null ||
                !JDM.HasFlags(m.Flags, "Active") ||
                !JDM.HasFlags(m.Flags, "Article") ||
                JDM.DateTime(m.PublishedOn) > DateTime.UtcNow)
            {
                return HttpNotFound();
            }
            return View(m);
        }
    }
}
